package com.ffyc.resale.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.dao.BuyDao;
import com.ffyc.resale.dao.GoodsDao;
import com.ffyc.resale.dao.UserDao;
import com.ffyc.resale.model.Buy;
import com.ffyc.resale.model.Data;
import com.ffyc.resale.model.Goods;
import com.ffyc.resale.model.User;
import com.ffyc.resale.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BuyService {
    @Autowired
    BuyDao buyDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    UserDao userDao;
    //查询所有
    public IPage<Buy> list(Buy buy){
        Page<Buy> page = new Page<>(buy.getPageNo(),buy.getPageSize());
        IPage<Buy> list = buyDao.selectPage(page,null);
        List<Buy> buys = list.getRecords();
        for(Buy b:buys){
            Integer goodsid = b.getGoodsid();
            b.setGoods(goodsDao.selectById(goodsid));
            b.setUserBuyName(userDao.selectById(b.getUserbuy()).getName());
            b.setUserSellName(userDao.selectById(b.getUsersell()).getName());
        }
        return list;
    }

    public IPage<Buy> search(Buy buy) {
        Page<Buy> page = new Page<>(buy.getPageNo(),buy.getPageSize());
        QueryWrapper<Buy> queryWrapper = new QueryWrapper<Buy>().eq(buy.getStatus()!=null,"status",buy.getStatus()).like(buy.getId()!=null,"id",buy.getId());
        IPage<Buy> list = buyDao.selectPage(page,queryWrapper);
        List<Buy> buys = list.getRecords();
        for(Buy b:buys){
            Integer goodsid = b.getGoodsid();
            b.setGoods(goodsDao.selectById(goodsid));
            b.setUserBuyName(userDao.selectById(b.getUserbuy()).getName());
            b.setUserSellName(userDao.selectById(b.getUsersell()).getName());
        }
        return list;
    }

    public void deleteById(Integer id) {
        buyDao.deleteById(id);
    }

    public IPage<Buy> getOrderList(String userToken, Buy buy) {
        Page<Buy> page = new Page<>(buy.getPageNo(),buy.getPageSize());
        page.addOrder(new OrderItem("create_time",false));
        Integer userbuy = JWTUtil.id(userToken);
        QueryWrapper<Buy> queryWrapper = new QueryWrapper<Buy>().eq("userbuy", userbuy).eq(buy.getStatus()!=null,"status",buy.getStatus());
        IPage<Buy> list = buyDao.selectPage(page,queryWrapper);
        List<Buy> buys = list.getRecords();
        for(Buy b:buys){
            Integer goodsid = b.getGoodsid();
            b.setGoods(goodsDao.selectById(goodsid));
            b.setUserBuyName(userDao.selectById(b.getUserbuy()).getName());
            b.setUserSellName(userDao.selectById(b.getUsersell()).getName());
        }
        return list;
    }

    public void updateStatus(Integer id, Integer status) {
        Buy buy = new Buy();
        buy.setId(id);
        buy.setStatus(status);
        if (status==1){
            Goods goods = new Goods();
            goods.setId(buyDao.selectById(id).getGoodsid());
            int num = goodsDao.selectById(goods.getId()).getNum();
            goods.setNum(num-1);
            goodsDao.updateById(goods);
        }
        if (status==4){
            Goods goods = buyDao.selectById(id).getGoods();
            goods.setNum(goods.getNum()+1);
            goodsDao.updateById(goods);
        }
        buyDao.updateById(buy);
    }

    public IPage<Buy> getSaleList(String userToken, Buy buy) {
        Page<Buy> page = new Page<>(buy.getPageNo(),buy.getPageSize());
        page.addOrder(new OrderItem("create_time",false));
        Integer usersell = JWTUtil.id(userToken);
        QueryWrapper<Buy> queryWrapper = new QueryWrapper<Buy>().eq("usersell", usersell).eq(buy.getStatus()!=null,"status",buy.getStatus());
        IPage<Buy> list = buyDao.selectPage(page,queryWrapper);
        List<Buy> buys = list.getRecords();
        for(Buy b:buys){
            Integer goodsid = b.getGoodsid();
            b.setGoods(goodsDao.selectById(goodsid));
            b.setUserBuyName(userDao.selectById(b.getUserbuy()).getName());
            b.setUserSellName(userDao.selectById(b.getUsersell()).getName());
        }
        return list;
    }

    public void buyGoods(Buy buy) {
        Date date = new Date();

        // 创建 SimpleDateFormat 实例，指定格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 格式化日期
        String formattedDate = formatter.format(date);
        buy.setCreateTime(formattedDate);
        buy.setStatus(1);
        Goods goods = goodsDao.selectById(buy.getGoodsid());
        goods.setNum(goods.getNum()-1);
        if (goods.getNum()<=0){
            goods.setStatus(3);
        }
        goodsDao.updateById(goods);
        buyDao.insert(buy);
    }
}
